package club.xyes.zkh.ecommerce.web.api.controller;

import club.xyes.zkh.ecommerce.commons.entity.WxFormId;
import club.xyes.zkh.ecommerce.commons.properties.WechatConfigurationProperties;
import club.xyes.zkh.ecommerce.commons.vo.GeneralResult;
import club.xyes.zkh.ecommerce.service.WxFormIdService;
import club.xyes.zkh.ecommerce.web.controller.BaseController;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Create by 郭文梁 2019/5/6 0006 13:17
 * WxFormIdController
 * 微信FormID数据访问控制器
 *
 * @author 郭文梁
 * @data 2019/5/6 0006
 */
@RestController
@RequestMapping("/api/wx-form-id")
public class WxFormIdController extends BaseController<WxFormId> {
    private final WxFormIdService service;
    private final WechatConfigurationProperties wechatConfigurationProperties;

    @Autowired
    public WxFormIdController(WxFormIdService service, WechatConfigurationProperties wechatConfigurationProperties) {
        super(service);
        this.service = service;
        this.wechatConfigurationProperties = wechatConfigurationProperties;
    }

    /**
     * 收集一个FormId
     *
     * @param formId FormId
     * @return GR
     */
    @ApiOperation("收集一个FormId")
    @PostMapping("/collect")
    public GeneralResult<WxFormId> collect(@RequestParam("formId") String formId) {
        String openId = getRequiredUserInfo().getWxOpenId();
        String appId = wechatConfigurationProperties.getUserMicroProgramAppId();
        WxFormId res = service.collect(appId, openId, formId);
        return GeneralResult.ok(res);
    }

    /**
     * 收集多个FormId
     *
     * @param formIdList formId列表
     * @return GR
     */
    @ApiOperation("收集多个FormId")
    @PostMapping("/collect-all")
    public GeneralResult<List<WxFormId>> collectAll(@RequestBody List<String> formIdList) {
        String appId = wechatConfigurationProperties.getUserMicroProgramAppId();
        String openId = getRequiredUserInfo().getWxOpenId();
        List<WxFormId> res = service.collect(appId, openId, formIdList);
        return GeneralResult.ok(res);
    }
}
